Skip to content

fix(sessions): order sqlite sessions deterministically by update_time - #6432

Closed
Q01P wants to merge 4 commits into
google:mainfrom
Q01P:fix/sqlite-list-sessions-order
Closed

Q01P wants to merge 4 commits into
google:mainfrom
Q01P:fix/sqlite-list-sessions-order

Conversation

@Q01P

@Q01P Q01P commented Jul 20, 2026

Copy link
Copy Markdown

Fixes #6431

Summary

  • order SqliteSessionService.list_sessions() by update_time ASC with stable user_id / id tie-breakers, appended to both the user-scoped and app-wide SELECT queries
  • add a regression where a newly created but stale orphan session must sort before older-but-active sessions, and tied update_time values fall back to id

Why

list_sessions() previously executed without an ORDER BY, so consumers using sessions[-1] for the most recently active session could receive an arbitrary row depending on the SQLite query plan (in practice, primary-key/insertion order).

This mirrors the ordering contract settled in #6272 and implemented in #6276 for DatabaseSessionService; #6276's diff doesn't touch the sqlite backend, leaving the two backends inconsistent. Ordering follows update_time (not create_time) because orphan sessions can be newly created while having stale activity — update_time maps to the returned Session.last_update_time. If that contract changes in review, this should follow.

Testing

  • RED before fix: uv run --extra test pytest tests/unittests/sessions/test_session_service.py::test_sqlite_session_service_list_sessions_orders_by_update_time_then_id -q
    E     AssertionError: assert ['active_a', ...le', 'orphan'] == ['orphan', 'm...', 'active_b']
    E       At index 0 diff: 'active_a' != 'orphan'
    1 failed, 4 warnings in 2.80s
    
  • GREEN after fix: same focused test → 1 passed, 4 warnings in 2.02s
  • uv run --extra test pytest tests/unittests/sessions/test_session_service.py -q → 136 passed, 5 warnings
  • uv run --extra dev pyink --check src/google/adk/sessions/sqlite_session_service.py tests/unittests/sessions/test_session_service.py → 2 files would be left unchanged
  • uv run --extra dev ruff check src/google/adk/sessions/sqlite_session_service.py tests/unittests/sessions/test_session_service.py → All checks passed!
  • git diff --check → clean

@adk-bot adk-bot added the services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc label Jul 20, 2026
@rohityan rohityan added the needs review [Status] The PR/issue is awaiting review from the maintainer label Jul 22, 2026
@rohityan
rohityan requested a review from sasha-gitg July 22, 2026 18:24
@rohityan

Copy link
Copy Markdown
Collaborator

Hi @Q01P , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

@rohityan

Copy link
Copy Markdown
Collaborator

Hi @sasha-gitg , can you please review this. LGTM

@DeanChensj

Copy link
Copy Markdown
Collaborator

Hi @Q01P , thanks for your PR! The same functionality has been achieved by 53e1afb as a subset, thus closing this for now. Any future contribution is still highly welcome!

@DeanChensj DeanChensj closed this Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs review [Status] The PR/issue is awaiting review from the maintainer services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqliteSessionService.list_sessions() has the same missing ORDER BY as #6272

4 participants